OsmGt example

Imports and prepare input parameters

In [1]:
from IPython.display import display

from bokeh.plotting import output_notebook
from bokeh.plotting import show

import geopandas as gpd

from shapely.geometry import Point
from shapely.ops import linemerge

from easy_map_bokeh import EasyMapBokeh

from osmgt import OsmGt

from graph_tool.topology import shortest_path


output_notebook()


location = "Roanne"
Loading BokehJS ...

Get POIs

In [2]:
%%time

pois_gdf = OsmGt.pois_from_location(location).get_gdf()

display(pois_gdf.head(2))
2020-09-20 10:17:28 - OsmGtPoi        - INFO     : From location: Roanne
2020-09-20 10:17:28 - OsmGtPoi        - INFO     : Loading data...
2020-09-20 10:17:28 - OsmGtPoi        - INFO     : NominatimApi: Query 200:OK in 0.49 sec.
2020-09-20 10:17:29 - OsmGtPoi        - INFO     : OverpassApi: Query 200:OK in 0.86 sec.
2020-09-20 10:17:29 - OsmGtPoi        - INFO     : Formating data
2020-09-20 10:17:29 - OsmGtPoi        - INFO     : Prepare GeoDataframe
2020-09-20 10:17:29 - OsmGtPoi        - INFO     : GeoDataframe Ready
addr:postcode amenity atm change_machine name operator phone ref:FR:LaPoste source stamping_machine ... ref:EU:EVSE socket:type2 socket:typee bar covered social_facility:for type:FR:FINESS service:bicycle:Bicycle_Sales_and_Service service:bicycle:repair geometry
0 42300 post_office yes yes Roanne Principal La Poste 3631 07916A data.gouv.fr:LaPoste - 10/2012 yes ... NaN NaN NaN NaN NaN NaN NaN NaN NaN POINT (4.07225 46.04071)
1 NaN place_of_worship NaN NaN Chapelle Jean Puy NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN POINT (4.07073 46.03766)

2 rows × 122 columns

CPU times: user 142 ms, sys: 33.3 ms, total: 175 ms
Wall time: 1.48 s

Get Roads

In [3]:
%%time
roads_initialized = OsmGt.roads_from_location(
    location,
    mode="vehicle",
    additional_nodes=pois_gdf
)
roads_gdf = roads_initialized.get_gdf()

display(roads_gdf.head(2))
2020-09-20 10:17:29 - OsmGtRoads      - INFO     : From location: Roanne
2020-09-20 10:17:29 - OsmGtRoads      - INFO     : Loading data...
2020-09-20 10:17:29 - OsmGtRoads      - INFO     : NominatimApi: Query 200:OK in 0.12 sec.
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : OverpassApi: Query 200:OK in 1.09 sec.
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Rebuild network data
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Network cleaning...
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Starting: Adding new nodes on the network
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Find nearest line for each node
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Split line
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Topology lines checker: to add: 184, to split: 182
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Starting: Find intersections
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Done: Find intersections
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Build lines
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : Prepare GeoDataframe
2020-09-20 10:17:31 - OsmGtRoads      - INFO     : GeoDataframe Ready
highway lanes:forward maxspeed name oneway ref id osm_url topo_uuid topology ... horse EntityHand SubClasses alt_name turn:lanes source:name maxspeed:backward maxspeed:forward cycleway:right:oneway geometry
0 primary 2 50 Rue de Charlieu yes D 482 24035569 https://www.openstreetmap.org/way/24035569 1_forward unchanged ... NaN NaN NaN NaN NaN NaN NaN NaN NaN LINESTRING (4.08544 46.05234, 4.08546 46.05248...
1 primary NaN 50 Rue de Charlieu yes D 482 24035570 https://www.openstreetmap.org/way/24035570 2_forward unchanged ... NaN NaN NaN NaN NaN NaN NaN NaN NaN LINESTRING (4.09087 46.06639, 4.09084 46.06622...

2 rows × 53 columns

CPU times: user 821 ms, sys: 80.2 ms, total: 901 ms
Wall time: 2.09 s

Display roads and nodes

In [4]:
%%time
layers_to_add = [
    {
        "input_gdf": roads_gdf,
        "legend": "roads",
        "color": "black",
    },
    {
        "input_gdf": pois_gdf,
        "legend": "POIs",
        "style": "square",
        "color": "blue",
        "size": 9
    },
]

my_map = EasyMapBokeh(
    "My roads and POIs - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_map.figure)
CPU times: user 2.03 s, sys: 29.5 ms, total: 2.06 s
Wall time: 2.06 s

Check topology details

In [5]:
%%time

roads_topology_gdfs = roads_initialized.topology_checker()

lines_unchanged = roads_topology_gdfs["lines_unchanged"]
lines_added = roads_topology_gdfs["lines_added"]
lines_split = roads_topology_gdfs["lines_split"]
nodes_added = roads_topology_gdfs["nodes_added"]
intersections_added = roads_topology_gdfs["intersections_added"]

layers_to_add = [
    {
        "input_gdf": lines_unchanged,
        "legend": "roads unchanged",
        "color": "black",
    },
    {
        "input_gdf": lines_added,
        "legend": "roads added",
        "color": "green",
    },
    {
        "input_gdf": lines_split,
        "legend": "roads split",
        "color": "orange",
    },
    {
        "input_gdf": intersections_added,
        "legend": "Intersections added",
        "color": "brown",
    },
    {
        "input_gdf": nodes_added,
        "legend": "Nodes added",  # POIs here
        "style": "square",
        "color": "blue",
        "size": 9
    },
]

my_map = EasyMapBokeh(
    "Topology about my roads and POIs - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_map.figure)
2020-09-20 10:17:33 - OsmGtRoads      - INFO     : Prepare topology data
2020-09-20 10:17:33 - OsmGtRoads      - INFO     : GeoDataframe Ready
CPU times: user 3.2 s, sys: 31.4 ms, total: 3.23 s
Wall time: 3.24 s

Get the graph-tool network and display it

In [6]:
%%time
graph = roads_initialized.get_graph()

# a plot method has been added on OsmGT.
graph.plot()
2020-09-20 10:17:37 - OsmGtRoads      - INFO     : Prepare graph
2020-09-20 10:17:38 - OsmGtRoads      - INFO     : Graph to image
CPU times: user 10.9 s, sys: 43.1 ms, total: 11 s
Wall time: 7.04 s

Compute a shortest path

With graph-tools function

In [7]:
%%time
# now, we have to define a start point and a end point and get their wkt
start_node_topo_uuid = 47
end_node_topo_uuid = 63

# 'topo_uuid' is generated by osmgt (during the topology processing).
# Some roads has been split that's whyso this id has been created.
start_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
end_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == end_node_topo_uuid]

start_node_wkt = start_node_gdf.iloc[0]["geometry"].wkt
end_node_wkt = end_node_gdf.iloc[0]["geometry"].wkt

# the graph have some methods (graph-tools method always exists!) to find egdes, vertices... Let's use the .find_vertex_from_name(). the wkt is the vertex name...
source_vertex = graph.find_vertex_from_name(start_node_wkt)
target_vertex = graph.find_vertex_from_name(end_node_wkt)

# shortest path computing...
path_vertices, path_edges = shortest_path(
    graph,
    source=source_vertex,
    target=target_vertex,
    weights=graph.edge_weights  # weigth is based on line length
)

# get path by using edge names
roads_ids = [
    graph.edge_names[edge]
    for edge in path_edges
]

roads_gdf_copy = roads_gdf.copy(deep=True)
shortest_path_found = roads_gdf_copy[roads_gdf['topo_uuid'].isin(roads_ids)].to_crs(3857)['geometry'].to_list()
shortest_path_found_gdf = gpd.GeoDataFrame(index=[0], crs="EPSG:3857", geometry=[linemerge(shortest_path_found)])

layers_to_add = [
    {
        "input_gdf": shortest_path_found_gdf,
        "legend": "shortest_path",
        "color": "red",
        "line_width": 5
    },
    {
        "input_gdf": start_node_gdf,
        "legend": "source node",
        "color": "blue",
        "style": "circle",
        "size": 9
    },
    {
        "input_gdf": end_node_gdf,
        "legend": "target node",
        "color": "green",
        "style": "circle",
        "size": 9
    },
]

my_shortest_path_map = EasyMapBokeh(
    "My shortest path - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_shortest_path_map.figure)
CPU times: user 242 ms, sys: 13.1 ms, total: 255 ms
Wall time: 255 ms

With OsmGt function

In [8]:
%%time

start_node_topo_uuid = 47
end_node_topo_uuid = 63

start_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]
end_node_gdf = pois_gdf[pois_gdf['topo_uuid'] == end_node_topo_uuid]

start_node = start_node_gdf.iloc[0]["geometry"]
end_node = end_node_gdf.iloc[0]["geometry"]

shortest_paths = OsmGt.shortest_path_from_location(
    "Roanne",
    [
        (start_node, end_node),
        (start_node, end_node),  # duplicate pairs are cleaned
    ],
    mode="pedestrian"
)
layers_to_add = [
    {
        "input_gdf": shortest_paths[["geometry"]],
        "legend": "shortest_path",
        "color": "red",
        "line_width": 5
    },
    {
        "input_gdf": start_node_gdf,
        "legend": "source node",
        "color": "blue",
        "style": "circle",
        "size": 9
    },
    {
        "input_gdf": end_node_gdf,
        "legend": "target node",
        "color": "green",
        "style": "circle",
        "size": 9
    },
]

my_shortest_path_map = EasyMapBokeh(
    "My shortest path - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_shortest_path_map.figure)

display(shortest_paths)
2020-09-20 10:17:44 - OsmGtShortestPath - INFO     : Shortest path processing...
2020-09-20 10:17:44 - OsmGtShortestPath - INFO     : From location: Roanne
2020-09-20 10:17:44 - OsmGtShortestPath - INFO     : Loading data...
2020-09-20 10:17:44 - OsmGtShortestPath - INFO     : NominatimApi: Query 200:OK in 0.12 sec.
2020-09-20 10:17:45 - OsmGtShortestPath - INFO     : OverpassApi: Query 200:OK in 1.28 sec.
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Rebuild network data
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Network cleaning...
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Starting: Adding new nodes on the network
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Find nearest line for each node
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Split line
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Topology lines checker: to add: 2, to split: 2
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Starting: Find intersections
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Done: Find intersections
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Build lines
2020-09-20 10:17:46 - OsmGtShortestPath - INFO     : Prepare graph
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : Prepare GeoDataframe
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : GeoDataframe Ready
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : Compute shortest path from 2189 to 2188
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : Prepare GeoDataframe
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : GeoDataframe Ready
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : Prepare GeoDataframe
2020-09-20 10:17:47 - OsmGtShortestPath - INFO     : GeoDataframe Ready
source_node target_node osm_ids osm_urls geometry id
0 POINT (4.0697088 46.0410178) POINT (4.0757785 46.0315038) 99078739, 99078739, 99078739, 99211707, 992117... https://www.openstreetmap.org/way/99078739, ht... LINESTRING (4.06971 46.04102, 4.06966 46.04086... 0
CPU times: user 1.39 s, sys: 59.7 ms, total: 1.45 s
Wall time: 2.89 s

Compute an isochrone

Isochrone from times

In [9]:
%%time

start_node_topo_uuid = 47
source_node = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]

# 2 = 2 min ; 5 = 5 min ; 10 = 10 min
isochrones_polygon_values = {
    2: "#d9ef8b",
    5: "#fee08b",
    10: "#f46d43"
}

isochrones_lines_values = {
    2: "#668100",
    5: "#e2a803",
    10: "#962603"
}

trip_speed = 3  # km/h

location_point = source_node.iloc[0]["geometry"]
isochrones_polygons, isochrones_lines = OsmGt.isochrone_from_source_node(
    location_point,
    list(isochrones_polygon_values.keys()),
    trip_speed,
    mode="pedestrian"
)

isochrones_polygons["color"] = isochrones_polygons["iso_name"].map(lambda x: isochrones_polygon_values[x])
isochrones_lines["color"] = isochrones_lines["iso_name"].map(lambda x: isochrones_lines_values[x])


layers_to_add = [
    {
        "input_gdf": isochrones_polygons,
        "legend": "iso_name",
        "fill_color": "color",
        "line_color": "color",
        "fill_alpha": 0.5
    },
    {
        "input_gdf": source_node,
        "legend": "Source node",
        "style": "circle",
        "color": "red",
        "size": 5
    },
    {
        "input_gdf": isochrones_lines,
        "legend": "iso_name",
        "color": "color",
        "line_width": 2
    },
]

my_shortest_path_map = EasyMapBokeh(
    "Isochrones from times - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_shortest_path_map.figure)

print("\nIsochrones polygons output")
display(isochrones_polygons)

print("\nIsochrones lines output")
display(isochrones_lines.head(2))
2020-09-20 10:17:47 - OsmGtIsochrone  - INFO     : Isochrone processing...
2020-09-20 10:17:47 - OsmGtIsochrone  - INFO     : From bbox: (4.063869750653223, 46.03696451481586, 4.075547849346776, 46.04507078784993)
2020-09-20 10:17:47 - OsmGtIsochrone  - INFO     : Loading data...
2020-09-20 10:17:47 - OsmGtIsochrone  - INFO     : OverpassApi: Query 200:OK in 0.5 sec.
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Rebuild network data
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Network cleaning...
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Starting: Adding new nodes on the network
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Find nearest line for each node
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Split line
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Topology lines checker: to add: 1, to split: 1
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Starting: Find intersections
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Done: Find intersections
2020-09-20 10:17:48 - OsmGtIsochrone  - INFO     : Build lines
2020-09-20 10:17:49 - OsmGtIsochrone  - INFO     : OverpassApi: Query 200:OK in 1.05 sec.
2020-09-20 10:17:49 - OsmGtIsochrone  - INFO     : Prepare GeoDataframe
2020-09-20 10:17:49 - OsmGtIsochrone  - INFO     : GeoDataframe Ready
2020-09-20 10:17:49 - OsmGtIsochrone  - INFO     : Prepare graph
2020-09-20 10:17:50 - OsmGtIsochrone  - INFO     : Compute isochrone: 10 minutes => 500 meters
2020-09-20 10:17:50 - OsmGtIsochrone  - INFO     : Compute isochrone: 5 minutes => 250 meters
2020-09-20 10:17:50 - OsmGtIsochrone  - INFO     : Compute isochrone: 2 minutes => 100 meters
2020-09-20 10:17:53 - OsmGtIsochrone  - INFO     : GeoDataframe Ready
ERROR:bokeh.core.validation.check:E-1006 (NON_MATCHING_DATA_SOURCES_ON_LEGEND_ITEM_RENDERERS): LegendItem.label is a field, but renderer data sources don't match: LegendItem(id='1824', ...)
Isochrones polygons output
iso_name time_unit iso_distance distance_unit geometry id color
0 10 minutes 500 meters POLYGON ((4.07179 46.04027, 4.07179 46.04027, ... 0 #f46d43
1 10 minutes 500 meters POLYGON ((4.06439 46.04167, 4.06498 46.04153, ... 1 #f46d43
2 5 minutes 250 meters POLYGON ((4.06947 46.04000, 4.06947 46.04000, ... 2 #fee08b
3 2 minutes 100 meters POLYGON ((4.06863 46.04104, 4.06864 46.04106, ... 3 #d9ef8b
Isochrones lines output
highway junction name id osm_url topo_uuid topology maxspeed oneway source ... foot horse EntityHand SubClasses ref:FR:FANTOIR maxspeed:type iso_name iso_distance geometry color
1 pedestrian NaN Passage des Bains 357803019 https://www.openstreetmap.org/way/357803019 1011 unchanged NaN NaN NaN ... NaN NaN NaN NaN NaN NaN 10.0 500.0 LINESTRING (4.06532 46.03856, 4.06544 46.03862... #962603
3 secondary NaN Place Louis Flandre 375987158 https://www.openstreetmap.org/way/375987158 1023 unchanged 50 NaN NaN ... NaN NaN NaN NaN NaN NaN 10.0 500.0 LINESTRING (4.07277 46.04221, 4.07281 46.04217... #962603

2 rows × 33 columns

CPU times: user 5.19 s, sys: 724 ms, total: 5.91 s
Wall time: 6.62 s

Isochrone from distances

In [10]:
%%time
start_node_topo_uuid = 47  # change the value here to define a new source point to compute isochrones
source_node = pois_gdf[pois_gdf['topo_uuid'] == start_node_topo_uuid]

trip_speed = 3  # km/h

isochrones_polygon_values = {
    250: "#d9ef8b",
    500: "#fee08b",
    750: "#f46d43",
    1000: "#8B4513"
}
# 250 = 250 meters ; 500 = 500 meters ; etc

isochrones_lines_values = {
    250: "#668100",
    500: "#e2a803",
    750: "#962603",
    1000: "#000000"
}

location_point = source_node.iloc[0]["geometry"]
isochrones_polygons, isochrones_lines = OsmGt.isochrone_distance_from_source_node(
    location_point,
    list(isochrones_polygon_values.keys()),  # meters
    trip_speed,
    mode="pedestrian"
)

isochrones_polygons["color"] = isochrones_polygons["iso_distance"].map(lambda x: isochrones_polygon_values[x])
isochrones_lines["color"] = isochrones_lines["iso_distance"].map(lambda x: isochrones_lines_values[x])


layers_to_add = [
    {
        "input_gdf": isochrones_polygons,
        "legend": "iso_distance",
        "fill_color": "color",
        "line_color": "color",
        "fill_alpha": 0.5
    },
    {
        "input_gdf": isochrones_lines,
        "legend": "iso_distance",
        "color": "color",
        "line_width": 2
    },
    {
        "input_gdf": source_node,
        "legend": "Source node",
        "style": "circle",
        "color": "red",
        "size": 15
    },
]

my_shortest_path_map = EasyMapBokeh(
    "Isochrones from distance - from OsmGT (https://github.com/amauryval)",
    layers=layers_to_add
)
show(my_shortest_path_map.figure)
2020-09-20 10:17:53 - OsmGtIsochrone  - INFO     : Isochrone processing...
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : From bbox: (4.058030701306445, 46.032910932296, 4.0813868986935535, 46.0491234783672)
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Loading data...
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : OverpassApi: Query 200:OK in 0.35 sec.
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Rebuild network data
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Network cleaning...
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Starting: Adding new nodes on the network
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Find nearest line for each node
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Split line
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Topology lines checker: to add: 1, to split: 1
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Starting: Find intersections
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Done: Find intersections
2020-09-20 10:17:54 - OsmGtIsochrone  - INFO     : Build lines
2020-09-20 10:17:55 - OsmGtIsochrone  - INFO     : OverpassApi: Query 200:OK in 0.54 sec.
2020-09-20 10:17:55 - OsmGtIsochrone  - INFO     : Prepare GeoDataframe
2020-09-20 10:17:55 - OsmGtIsochrone  - INFO     : GeoDataframe Ready
2020-09-20 10:17:55 - OsmGtIsochrone  - INFO     : Prepare graph
2020-09-20 10:17:59 - OsmGtIsochrone  - INFO     : Compute isochrone: 20.0 minutes => 1000 meters
2020-09-20 10:18:00 - OsmGtIsochrone  - INFO     : Compute isochrone: 15.0 minutes => 750 meters
2020-09-20 10:18:00 - OsmGtIsochrone  - INFO     : Compute isochrone: 10.0 minutes => 500 meters
2020-09-20 10:18:00 - OsmGtIsochrone  - INFO     : Compute isochrone: 5.0 minutes => 250 meters
2020-09-20 10:18:08 - OsmGtIsochrone  - INFO     : GeoDataframe Ready
ERROR:bokeh.core.validation.check:E-1006 (NON_MATCHING_DATA_SOURCES_ON_LEGEND_ITEM_RENDERERS): LegendItem.label is a field, but renderer data sources don't match: LegendItem(id='2036', ...)
CPU times: user 17.5 s, sys: 135 ms, total: 17.6 s
Wall time: 15.6 s